package pro.softzhang.algo.lc0;

import pro.softzhang.algo.common.Utils;
import pro.softzhang.algo.flag.knowledge.KLang;
import pro.softzhang.algo.flag.theme.TMathArith;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@KLang
@TMathArith
public class LC1_TwoSum {
    public static void main(String[] args) {
        int[] nums = {3, 2, 4};
        int target = 6;
        Utils.printArr(new Solution().twoSum(nums, target));
    }

    /**
     * 
     */
    static
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> m = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                if (m.containsKey(target - nums[i])) {
                    return new int[]{m.get(target - nums[i]), i};
                } else {
                    m.put(nums[i], i);
                }
            }
            return new int[]{};
        }
    }
}
